package com.bkgy.modularbx.coil_track_stats.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bkgy.modularbx.coil_track_stats.domain.CoilCountDaily;
import com.bkgy.modularbx.coil_track_stats.dto.CoilCountStatsDTO;
import com.bkgy.modularbx.coil_track_stats.mapper.CoilCountDailyMapper;
import com.bkgy.modularbx.coil_track_stats.service.CoilCountDailyService;
import com.bkgy.modularbx.coil_track_stats.vo.TrackStatsQueryVO;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
* @author dell
* @description 针对表【T_COIL_COUNT_DAILY(每日分卷数统计表)】的数据库操作Service实现
* @createDate 2025-11-08 14:47:50
*/
@Service
public class CoilCountDailyServiceImpl extends ServiceImpl<CoilCountDailyMapper, CoilCountDaily>
    implements CoilCountDailyService{

    // 每根卷的固定成本
    private static double PER_COIL_COST = 31.5;

    @Override
    public CoilCountStatsDTO getCountStats(TrackStatsQueryVO queryVO) {
        // 转换产线类型
        TrackStatsQueryVO processedQuery = processLineType(queryVO);
        List<CoilCountDaily> statsList = baseMapper.selectCountStats(processedQuery);
        
        CoilCountStatsDTO result = new CoilCountStatsDTO();
        
        // 提取统计日期列表
        List<String> tocDates = statsList.stream()
            .map(item -> {
                if (queryVO.getDateType() == 1) {
                    return item.getStatDate();
                } else {
                    // 按月统计时，使用统计月份字段
                    return item.getStatMonth();
                }
            })
            .collect(Collectors.toList());
        
        // 提取推荐分卷数
        List<BigDecimal> recommendCounts = statsList.stream()
            .map(item -> BigDecimal.valueOf(item.getRecommendCount()))
            .collect(Collectors.toList());
        
        // 提取实际分卷数
        List<BigDecimal> actualCounts = statsList.stream()
            .map(item -> BigDecimal.valueOf(item.getActualCount()))
            .collect(Collectors.toList());
        
        // 提取新增费用
        List<BigDecimal> addedCosts = statsList.stream()
            .map(CoilCountDaily::getAddedCost)
            .collect(Collectors.toList());

        //获取可减少分卷数量
        List<Integer> slitDownCounts = statsList.stream()
                .map(item -> Optional.ofNullable(item.getSlitDownCount()).orElse(0))
                .collect(Collectors.toList());

        //优化费用
        List<BigDecimal> slitDownCosts = statsList.stream()
                .map(item -> Optional.ofNullable(item.getSlitDownCount()).orElse(0))
                .map(item -> item * PER_COIL_COST)
                .map(BigDecimal::valueOf)
                .collect(Collectors.toList());
        //封装结果
        result.setTocDates(tocDates);
        result.setRecommendCounts(recommendCounts);
        result.setActualCounts(actualCounts);
        result.setAddedCosts(addedCosts);
        result.setSlitDownCounts(slitDownCounts);
        result.setSlitDownCosts(slitDownCosts);
        
        return result;
    }

    /**
     * 处理产线类型转换
     * @param queryVO 原始查询参数
     * @return 处理后的查询参数
     */
    private TrackStatsQueryVO processLineType(TrackStatsQueryVO queryVO) {
        TrackStatsQueryVO processedQuery = new TrackStatsQueryVO();
        processedQuery.setDateType(queryVO.getDateType());
        processedQuery.setStartDate(queryVO.getStartDate());
        processedQuery.setEndDate(queryVO.getEndDate());
        
        // 产线类型转换逻辑
        String lineType = queryVO.getLineType();
        switch (lineType) {
            case "RCL2150":
                processedQuery.setLineType("RECOIL1");
                break;
            case "RCL1630":
                processedQuery.setLineType("RECOIL2");
                break;
            case "RCL1870":
                processedQuery.setLineType("RECOIL3");
                break;
            default:
                processedQuery.setLineType(lineType);
                break;
        }
        
        return processedQuery;
    }
}




